﻿Public Class Buscar_Clientes
    Dim datos_clase As New datos

    Private Sub cargarClientes()
        Dim conexion As New SqlClient.SqlConnection
        Dim cmd As New SqlClient.SqlCommand
        Dim data As New Data.DataTable
        conexion.ConnectionString = datos_clase._cadena
        conexion.Open()
        cmd.Connection = conexion
        cmd.CommandType = CommandType.Text
        Dim sql As String = "SELECT tipo_documento.nombre , cliente.numero_documento, cliente.nombre, cliente.apellido FROM (cliente INNER JOIN tipo_documento on cliente.id_tipo_documento = tipo_documento.id_tipo_documento)"
        cmd.CommandText = sql
        data.Load(cmd.ExecuteReader())
        conexion.Close()
        Dim fila As Integer = 0

        DataGridView1.Rows.Clear()

        For fila = 0 To data.Rows.Count - 1
            DataGridView1.Rows.Add()
            DataGridView1.Rows(fila).Cells("tipo_doc").Value = data.Rows(fila)(0)
            DataGridView1.Rows(fila).Cells("numero_documento").Value = data.Rows(fila)(1)
            DataGridView1.Rows(fila).Cells("nombre").Value = data.Rows(fila)(2)
            DataGridView1.Rows(fila).Cells("apellido").Value = data.Rows(fila)(3)
        Next
    End Sub

    Private Sub Buscar_Clientes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargarClientes()
    End Sub

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        Dim temp As String = "nombre = '" & DataGridView1.CurrentRow.Cells(0).Value & "'"
        Dim tabla_doc As Data.DataTable = datos_clase.leo_tabla("tipo_documento", temp)
        Dim condicion As String = "numero_documento = '" & DataGridView1.CurrentRow.Cells(1).Value _
                                       & "' and id_tipo_documento = '" & tabla_doc.Rows(0)(0) & "'"
        Dim tabla As Data.DataTable = datos_clase.leo_tabla("cliente", condicion)

        Factura.txt_documento.Text = tabla.Rows(0)(0)
        Factura.cmb_tipo_documento.SelectedValue = tabla.Rows(0)(1)
        Factura.txt_apellido_cliente.Text = tabla.Rows(0)(2)
        Factura.txt_nombre_cliente.Text = tabla.Rows(0)(3)

        Me.Close()
    End Sub

    Private Sub btn_salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_salir.Click
        Me.Close()
    End Sub
End Class